﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Diagnostics;
namespace Async.Compute.Bound
{
    class TimeDemo
    {
        private Timer timer;

        public void Go()
        {
            Trace.WriteLine("Main thread: starting a timer");
            using (timer = new Timer(ComputeBoundOp, 5, 0, Timeout.Infinite))
            {
                Trace.WriteLine("Main thread: doing other work here...");
                Thread.Sleep(1000);
            }//using 结束时调用dispose取消计时器
        }

        private void ComputeBoundOp(object state)
        {
            Trace.WriteLine(string.Format("In ComputeBoundOp state:={0}", state));
            Thread.Sleep(1000);
            //让Timer在2秒之后再次调用这个方法
            timer.Change(2000, Timeout.Infinite);
            //这个方法返回后,线程回归线程池,等待下一个工作项.
        }
    }
}
